﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataContracts;
using ServiceContracts;
using System.ServiceModel;

namespace Presention
{
    public enum RoleStatus
    {
        InUse = 1,
        NoUse = 2
    }
    public partial class BatchUpdateRoleStatus : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                using (ServiceAgent proxy = new ServiceAgent())
                {
                    DropDownList1.DataSourceID = null;

                    DropDownList1.DataSource = proxy.ServiceClient.GetAllRoleStatus();
                    DropDownList1.DataBind();

                    DropDownList1.Items.Insert(0, new ListItem("select", "0"));
                    databind();
                }
            }
        }
        private void databind()
        {
            using (ServiceAgent proxy = new ServiceAgent())
            {
                string roleName = "", status = "0";

                if (txtRolename.Text.Trim().Length > 0)
                    roleName = txtRolename.Text.Trim();
                if (DropDownList1.SelectedValue != "0")
                    status = DropDownList1.SelectedValue;


                rptlist.DataSourceID = null;
                rptlist.DataSource = proxy.ServiceClient.GetAllRole(txtRolename.Text.Trim(), int.Parse(DropDownList1.SelectedValue));//iRoleService.GetAllRole(txtRolename.Text.Trim(), int.Parse(DropDownList1.SelectedValue));
                try
                {
                    rptlist.DataBind();
                }
                catch (FaultException ex)
                {
                    Response.Write(" <script language=javascript> alert( ' " + ex.Message + " ') </script> ");

                }

            }

        }
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());
            try
            {
                using (ServiceAgent proxy = new ServiceAgent())
                {

                    if (e.CommandName == "del")
                    {
                        proxy.ServiceClient.DeleteRole(id);
                        databind();
                    }


                    if (e.CommandName == "upt")
                    {
                        string url;
                        url = "RoleDetails.aspx?roleid_trans=" + id;
                        Response.Redirect(url);
                    }

                }
            }
            catch (FaultException ex)
            {


                Response.Write(" <script language=javascript> alert( ' " + ex.Message + " ') </script> ");


            }
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Button b1 = (Button)e.Item.FindControl("btndel");

            if (b1 != null)
            {
                b1.Attributes.Add("onclick", "return confirm(\"Delete This Data?\");");
            }

        }



        protected void btninuse_Click(object sender, EventArgs e)
        {
            try
            {

                using (ServiceAgent proxy = new ServiceAgent())
                {
                    List<RoleDTO> roleList = new List<RoleDTO>();
                    RoleDTO role;
                    for (int i = 0; i < this.rptlist.Items.Count; i++)
                    {
                        CheckBox chb = (rptlist.Items[i].FindControl("chks")) as CheckBox;

                        if (chb.Checked == true)
                        {
                            int roleId = int.Parse(((rptlist.Items[i].FindControl("HiddenField1")) as HiddenField).Value);
                            role = proxy.ServiceClient.GetRoleById(roleId);
                            role.Status = new StatusDTO();


                            role.Status.StatusId = (int)RoleStatus.InUse;
                            roleList.Add(role);

                        }
                    }
                    proxy.ServiceClient.BatchUpdateRoleStatus(roleList);
                    databind();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void addrole_Click(object sender, EventArgs e)
        {
            string url;
            url = "RoleDetails.aspx?roleid_trans=" + 0;
            Response.Redirect(url);
        }

        protected void btnnouse_Click(object sender, EventArgs e)
        {
            try
            {
                using (ServiceAgent proxy = new ServiceAgent())
                {
                    List<RoleDTO> roleList = new List<RoleDTO>();
                    RoleDTO role;
                    for (int i = 0; i < this.rptlist.Items.Count; i++)
                    {
                        CheckBox chb = (rptlist.Items[i].FindControl("chks")) as CheckBox;
                        if (chb.Checked == true)
                        {
                            int roleId = int.Parse(((rptlist.Items[i].FindControl("HiddenField1")) as HiddenField).Value);
                            role = proxy.ServiceClient.GetRoleById(roleId);
                            role.Status = new StatusDTO();


                            role.Status.StatusId = (int)RoleStatus.NoUse;
                            roleList.Add(role);

                        }

                    }
                    proxy.ServiceClient.BatchUpdateRoleStatus(roleList);
                    databind();

                }
            }

            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            using (ServiceAgent proxy = new ServiceAgent())
            {
                List<RoleDTO> roleList = proxy.ServiceClient.GetAllRole(txtRolename.Text.Trim(), int.Parse(DropDownList1.SelectedValue));
                if (roleList.Count == 0)
                {
                    Response.Write("<script language = javascript>alert('The Role not in database');</script>");

                }
                else
                    databind();
            }
        }
    }
}